<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <!-- 条件栏 -->
    <page-filter
      v-model:query="filterInfo.query"
      :filter-list="filterInfo.list"
    />
    <page-table
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="api"
      :query="filterInfo.query"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :highlight-row="highlightRow"
      :selectable="selectable"
      :checkedKey="false"
      :reserve-selection="true"
      :check-box="checkBox"
      :row-class-name="tableRowClassName"
      @handleEvent="handleEvent"
    >
      <template v-slot:col-licenseNumber="scope">
        <p>{{ scope.row.licenseNumber || "-" }}</p>
        <p>{{ scope.row.brandName || "-" }}</p>
      </template>
      <template v-slot:col-commonName="scope">
        <p>{{ scope.row.commonName || "-" }}</p>
        <p>{{ scope.row.productCode || "-" }}</p>
        <p>{{ scope.row.alias || "-" }}</p>
      </template>
      <template v-slot:col-type="scope">
        <p>{{ scope.row.productTypeValue || "-" }}</p>
        <p>{{ scope.row.codeTypeValue || "-" }}</p>
        <p>{{ scope.row.productCategoryValue || "-" }}</p>
      </template>
      <template v-slot:col-standard="scope">
        <p>{{ scope.row.standard || "-" }}</p>
        <p>{{ scope.row.unitName || "-" }}</p>
      </template>
    </page-table>
  </page-dialog>
</template>
<script setup name="selectProduct">
import PageDialog from "@/components/PageDialog/index";
import PageTable from "@/components/PageTable/index";
import PageFilter from "@/components/PageFilter/index";
import { useApplicationDetailPage } from "@/api/use";

const { proxy } = getCurrentInstance();
const emit = defineEmits();
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Function,
    default: useApplicationDetailPage,
  },
  showDialog: {
    type: Boolean,
    default: false,
  },
  query: {
    type: Object,
    default: function () {
      return {};
    },
  },
  showStock: {
    type: Boolean,
    default: false,
  },
});

const shows = computed({
  get() {
    return props.showDialog;
  },
  set(newValue) {
    emit("update:showDialog", newValue); // 更新父组件shows
  },
});

const selectRow = ref("");
// 过滤相关配置
const filterInfo = reactive({
  query: {
    searchType: "product",
    status: "enabled",
    sort: "common_name,standard",
    ...props.query,
  },
  list: [
    {
      type: "input",
      label: "名称/别名/编码/助记符",
      value: "keyword",
      event: search,
    },
    { type: "button", label: "搜索", event: search },
  ],
});
// 表格相关
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [
    {
      label: "通用名称\n编码\n别名",
      value: "commonName",
      minWidth: 220,
      type: "slot",
    },
    { label: "规格\n计量单位", value: "standard", type: "slot" },
    {
      label: "领出仓库存量",
      value: "totalStockQty",
      type: "column",
      formatter: proxy.$fn.qtyFormatter,
    },
    {
      label: "领入仓库存量",
      value: "inStorageQty",
      type: "column",
      formatter: proxy.$fn.qtyFormatter,
    },
    {
      label: "单价",
      value: "defaultSupplierPrice",
      type: "column",
      formatter: proxy.$fn.TripartiteMethod,
    },
    {
      label: "产品类型\n条码类型\n产品分类",
      value: "type",
      minWidth: 150,
      type: "slot",
    },
    { label: "助记符", value: "mnemonic", width: "100px" },
    {
      label: "证号\n生产厂家",
      value: "licenseNumber",
      minWidth: 220,
      type: "slot",
    },
  ],
});
// 弹窗相关
const dialogInfo = reactive({
  title: "选择产品",
  width: "90%",
  btLoading: false,
  btList: [
    { label: "关闭", event: handleClose, show: true },
    {
      label: "保存",
      type: "primary",
      disabled: true,
      event: save,
      show: true,
    },
  ],
});

onMounted(() => {
  // hack, 在table创建出来之后再去调用
  proxy.$nextTick(() => {
    if (props.showStock) {
      tableInfo.fieldList.push({
        label: "库存数量",
        value: "stockQty",
        type: "column",
        formatter: proxy.$fn.qtyFormatter,
      });
    }
    getList();
  });
});

// 获取列表
function getList() {
  tableInfo.refresh = Math.random();
}
function search() {
  tableInfo.initCurpage = Math.random();
  tableInfo.refresh = Math.random();
}
function save() {
  // emit('input', data)
  if (selectRow.value) {
    handleClose();
    emit("callback", selectRow.value);
  } else {
    proxy.$message({
      type: "warning",
      duration: 3500,
      showClose: true,
      message: "请选择产品",
    });
  }
}
function handleEvent(event, data) {
  switch (event) {
    case "tableClickRow":
      selectRow.value = data;
      dialogInfo.btList[1].disabled = !selectRow.value;
      break;
    case "tableCheck":
      selectRow.value = data;
      dialogInfo.btList[1].disabled = !data.length;
      break;
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false;
  emit("reset");
}
function selectable(row) {
  if (row.disabled) {
    return false;
  }
  return true;
}
function tableRowClassName({ row }) {
  if (row.warningStockStatus === "high") {
    return "nearly-row";
  } else if (row.warningStockStatus === "low") {
    return "expire-row";
  }
  return "";
}
</script>

<style scoped lang="scss">
:deep(.el-table .nearly-row) {
  background: #fceacb;
}
:deep(.el-table .expire-row) {
  background: #f5e4e4;
}
</style>
